package com.xinqi.modules.user.users.user.controller.inner;

import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.user.user.dto.req.*;
import com.xinqi.modules.user.user.dto.rsp.SearchFriendResultDTO;
import com.xinqi.modules.user.user.dto.rsp.UserBaseInfoDO;
import com.xinqi.modules.user.user.dto.rsp.UserPasswordResultDTO;
import com.xinqi.modules.user.user.dto.rsp.UserResultDTO;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.user.users.user.service.UserService;
import io.swagger.v3.oas.annotations.Hidden;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;

/**
 * 用户信息内部控制层
 *
 * @author: wenjie
 * @date: 2023/05/17
 */
@Hidden
@RestController
@RequestMapping("/inner/user")
@RequiredArgsConstructor
public class UserInnerController {
    private final UserService userService;

    /**
     * 关键字搜索 -- 新奇号、手机号精准查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/search")
    public R<List<SearchFriendResultDTO>> search(@RequestBody SearchFriendDTO dto) {
        return R.data(userService.selectUsersByKeyword(dto));
    }

    @GetMapping("/list")
    public R<List<UserResultDTO>> list(@RequestBody UserQueryDTO dto) {
        List<UserResultDTO> list = userService.list(dto);
        return R.data(list);
    }

    @GetMapping("/get/{userId}")
    public R<UserResultDTO> findById(@NotNull(message = "{user.id.not.black}") @PathVariable("userId") Long userId) {
        return R.ok(userService.selectById(userId));
    }

    /**
     * 获取用户信息包含密码
     */
    @GetMapping("/get-password/{userId}")
    public R<UserPasswordResultDTO> findPasswordById(@NotNull(message = "{user.id.not.black}") @PathVariable("userId") Long userId) {
        return R.ok(userService.selectPasswordById(userId));
    }

    @PostMapping("/findList")
    public R<List<UserResultDTO>> findList(@RequestParam("userIds") List<Long> userIds) {
        return R.data(userService.findList(userIds));
    }

    /**
     * 根据手机号查询用户
     *
     * @param mobile 手机号
     * @return R<UserResultDTO>
     */
    @GetMapping("/find-by-mobile")
    public R<UserResultDTO> findByMobile(@RequestParam("mobile") String mobile) {
        return R.data(userService.findByMobile(mobile));
    }

    /**
     * 查询用户基础信息通过手机号-包含已注销用户
     * @param mobile 用户手机号
     * @return 用户基础信息
     */
    @GetMapping("/user-account/contain/delete")
    public R<List<UserBaseInfoDO>> getUserBaseInfoContainDelByMobile(String mobile) {
        return R.ok(userService.findListByMobileIncludeDelete(mobile));
    }

    /**
     * 根据手机号和昵称查询用户
     *
     * @param mobile 手机号
     * @return R<UserResultDTO>
     */
    @GetMapping("/find-imitate-by-mobile-and-nickname")
    public R<UserResultDTO> findImitateByMobileAndNickname(@RequestParam("mobile") String mobile, @RequestParam("nickname") String nickname) {
        return R.data(userService.findImitateByMobileAndNickname(mobile, nickname));
    }

    /**
     * 查询用户基础
     *
     * @param userId 用户id
     * @return 用户基础信息
     */
    @GetMapping("/user-account")
    public R<UserBaseInfoDO> getUserBaseInfo(Long userId) {
        UserBaseInfoDO userBaseInfo = Optional.ofNullable(userService.findUserBaseInfo(userId))
            .orElseThrow(() -> new RuntimeException("用户不存在"));
        return R.data(userBaseInfo);
    }

    @PostMapping(value = "/register")
    public R<UserResultDTO> registerPlatForm(@RequestBody UserRegisterDTO register) {
        return R.ok(userService.registerXqAppUser(register));
    }

    /**
     * 账号封禁
     *
     * @param userIds
     * @return
     */
    @PostMapping(value = "/accountBan")
    public R<Boolean> accountBan(@RequestParam("userIds") List<Long> userIds) {
        return R.ok(userService.accountBan(userIds));
    }

    /**
     * 账号解封
     *
     * @param userIds
     * @return
     */
    @PostMapping(value = "/accountUnblocking")
    public R<Boolean> accountUnblocking(@RequestParam("userIds") List<Long> userIds) {
        return R.ok(userService.accountUnblocking(userIds));
    }

    @PostMapping(value = "/findByMobile")
    public R<List<UserResultDTO>> findByMobile(@RequestParam("mobileList") List<String> mobileList) {
        return R.data(userService.findByMobile(mobileList));
    }

    /**
     * 校验手机号码是否为新用户
     *
     * @param mobile
     * @return
     */
    @PostMapping(value = "/findByMobileIncludeDelete")
    public R<Boolean> findByMobileIncludeDelete(@RequestParam("mobile") String mobile) {
        return R.data(userService.findByMobileIncludeDelete(mobile));
    }

    /**
     * 注册助手用户
     *
     * @return
     */
    @PostMapping("/assistant/register")
    public R<UserResultDTO> registerAssistant() {
        return R.ok(userService.registerAssistant());
    }

    /**
     * 注册模拟用户
     *
     * @param dto
     * @return
     */
    @PostMapping("/imitate/register")
    public R<UserResultDTO> imitateUser(@RequestBody UserImitateDTO dto) {
        return R.ok(userService.imitateRegister(dto));
    }

    /**
     * 修改模拟用户信息
     *
     * @param dto
     * @return
     */
    @PostMapping("/imitate/update")
    public R<Void> imitateUserUpdate(@RequestBody UserImitateUpdateDTO dto) {
        userService.imitateUserUpdate(dto);
        return R.ok("修改成功");
    }


    /**
     * 兼容旧版本强制更新
     * 添加临时 client
     * @param userId
     * @return
     */
    @GetMapping("/getInfo/{userId}")
    public R<UserResultDTO> getInfo(@PathVariable("userId") Long userId) {
        return R.ok(userService.selectById(userId));
    }

}
